package com.ruoyi.mes.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.mes.domain.MesTasks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.mes.domain.MesWorkWorkers;
import com.ruoyi.mes.mapper.MesWorkMapper;
import com.ruoyi.mes.domain.MesWork;
import com.ruoyi.mes.service.IMesWorkService;

import javax.annotation.Resource;

/**
 * 加工单Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-13
 */
@Service
public class MesWorkServiceImpl implements IMesWorkService 
{
    @Resource
    private MesWorkMapper mesWorkMapper;

    /**
     * 查询加工单
     * 
     * @param mesWorkUuid 加工单主键
     * @return 加工单
     */
    @Override
    public MesWork selectMesWorkByMesWorkUuid(String mesWorkUuid)
    {
        return mesWorkMapper.selectMesWorkByMesWorkUuid(mesWorkUuid);
    }

    /**
     * 查询加工单列表
     * 
     * @param mesWork 加工单
     * @return 加工单
     */
    @Override
    public List<MesWork> selectMesWorkList(MesWork mesWork)
    {
        return mesWorkMapper.selectMesWorkList(mesWork);
    }


    /**
     * 派工时实际可填写数量
     *
     * @param mesWork 实际可派工数量
     * @return 派工数量
     */
    @Override
    public BigDecimal selectRemainWorkQty(MesWork mesWork)
    {
        return mesWorkMapper.selectRemainWorkQty(mesWork);
    }
    /**
     * 新增加工单
     * 
     * @param mesWork 加工单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesWork(MesWork mesWork)
    {
        mesWork.setCreateBy(SecurityUtils.getUsername());
        mesWork.setCreateTime(DateUtils.getNowDate());
        int rows = mesWorkMapper.insertMesWork(mesWork);
        insertMesWorkWorkers(mesWork);
        return rows;
    }

    /**
     * 修改加工单
     * 
     * @param mesWork 加工单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesWork(MesWork mesWork)
    {
        mesWork.setUpdateTime(DateUtils.getNowDate());
        mesWorkMapper.deleteMesWorkWorkersByFkMesWorkUuid(mesWork.getMesWorkUuid());
        insertMesWorkWorkers(mesWork);
        return mesWorkMapper.updateMesWork(mesWork);
    }

    /**
     * 批量删除加工单
     * 
     * @param mesWorkUuids 需要删除的加工单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesWorkByMesWorkUuids(String[] mesWorkUuids)
    {
        mesWorkMapper.deleteMesWorkWorkersByFkMesWorkUuids(mesWorkUuids);
        return mesWorkMapper.deleteMesWorkByMesWorkUuids(mesWorkUuids);
    }

    /**
     * 删除加工单信息
     * 
     * @param mesWorkUuid 加工单主键
     * @return 结果
     */
    @Override
    public int deleteMesWorkByMesWorkUuid(String mesWorkUuid)
    {
        mesWorkMapper.deleteMesWorkWorkersByFkMesWorkUuid(mesWorkUuid);
        return mesWorkMapper.deleteMesWorkByMesWorkUuid(mesWorkUuid);
    }

    /**
     * 新增加工单（员工）信息
     * 
     * @param mesWork 加工单对象
     */
    public void insertMesWorkWorkers(MesWork mesWork)
    {

        List<MesWorkWorkers> mesWorkWorkersList = mesWork.getMesWorkWorkersList();
        String mesWorkUuid = mesWork.getMesWorkUuid();
        if (StringUtils.isNotNull(mesWorkWorkersList))
        {
            List<MesWorkWorkers> list = new ArrayList<MesWorkWorkers>();
            for (MesWorkWorkers mesWorkWorkers : mesWorkWorkersList)
            {
                mesWorkWorkers.setFkMesWorkUuid(mesWorkUuid);
                list.add(mesWorkWorkers);
            }
            if (list.size() > 0)
            {
                mesWorkMapper.batchMesWorkWorkers(list);
            }
        }
    }
}
